<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['repertory:PdmMstr:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['repertory:PdmMstr:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="single"
          @click="handleDelete"
          v-hasPermi="['repertory:PdmMstr:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-check"
          size="mini"
          :disabled="single"
          @click="handleSubmit"
          v-hasPermi="['repertory:PdmMstr:submit']"
        >
          提交
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport('#exportPdm', '库存盘点数据查询-汇总')"
          v-hasPermi="['repertory:PdmMstr:export']"
        >
          Excel
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="exportTotal"
          v-hasPermi="['repertory:PdmMstr:export']"
        >
          Excel子项
        </el-button>
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      v-loading="loading"
      :height="tableH"
      id="exportPdm"
      :cell-style="iCellStyle"
      :data="PdmMstrList"
      @row-click="clickRow"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" type="index" width="40" align="center" />
      <el-table-column
        label="盘点单号"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pdmDjbh"
      />
      <el-table-column
        label="盘点日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pdmPdrq"
        width="250"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pdmPdrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="盘点客户"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pdmKhdm"
      >
        <template #default="scope">
          <dict-tag :options="allCustomerOptions" :value="scope.row.pdmKhdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="调整仓库"
        :show-overflow-tooltip="true"
        align="center"
        prop="pdmCkdm"
      >
        <template #default="scope">
          <dict-tag :options="allStorageOptions" :value="scope.row.pdmCkdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pdmDesc"
      />
      <el-table-column
        label="撤回原因"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pdmNgdesc"
      />
      <el-table-column
        label="开单人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pdmJlry"
      />
      <el-table-column
        label="开单日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pdmJlrq"
        width="250"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pdmJlrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="确认人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pdmQrry"
      />
      <el-table-column
        label="确认日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pdmQrrq"
        width="250"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pdmQrrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="批准人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pdmPzry"
      />
      <el-table-column
        label="批准日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pdmPzrq"
        width="250"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pdmPzrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="过账人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pdmGzry"
      />
      <el-table-column
        label="过账日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pdmGzrq"
        width="250"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pdmGzrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
    </el-table>
    <u-table
      border
      stripe
      use-virtual
      :row-height="24"
      :height="tableHChild"
      :data="pddDetList"
      :cell-style="iCellStyle"
      :header-cell-style="{ 'text-align': 'center' }"
      id="exportPdd"
    >
      <el-table-column
        label="#"
        type="index"
        width="40"
        align="center"
        fixed="left"
      />
      <el-table-column
        label="物料代码"
        width="135"
        align="center"
        prop="pddWldm"
      />
      <el-table-column
        label="客户编码"
        width="200"
        align="center"
        prop="pddCustWldm"
      />
      <el-table-column label="规格描述" min-width="335" prop="pddPmgg" />
      <el-table-column
        label="单位"
        :show-overflow-tooltip="true"
        width="60"
        align="center"
        prop="pddDw"
      >
        <template #default="scope">
          <dict-tag :options="unitFlagOptions" :value="scope.row.pddDw" />
        </template>
      </el-table-column>
      <el-table-column
        label="存放位置"
        width="135"
        align="center"
        prop="pddCwdm"
      >
        <template #default="scope">
          <dict-tag :options="allStorageOptions" :value="scope.row.pddCwdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="库存数量"
        width="80"
        align="center"
        prop="pddKcsl"
      />
      <el-table-column
        label="实盘数量"
        width="80"
        align="center"
        prop="pddPdsl"
      />
      <el-table-column
        label="盘点差异"
        width="80"
        align="center"
        prop="pddCysl"
      />
      <el-table-column
        label="盘点人员"
        width="80"
        align="center"
        prop="pddPdry"
      />
      <el-table-column
        label="盘点日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pddPdrq"
        width="150"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pddPdrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
    </u-table>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="650px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="24">
          <el-form-item label="开单日期">
            <el-date-picker
              v-model="dateRange"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="盘点单号" prop="pdmDjbh">
            <el-input
              v-model="queryParams.pdmDjbh"
              placeholder="请输入盘点单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="盘点仓库" prop="pdmCkdm">
            <el-select
              v-model="queryParams.pdmCkdm"
              filterable
              clearable
              placeholder="请选择盘点仓库"
            >
              <el-option
                v-for="item in storageOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item>
            <RadioGroup v-model="queryParams.pdmZtbz">
              <Radio label="1" style="margin: 0px 30px">未确认</Radio>
              <Radio label="2" style="margin-right: 30px">已确认</Radio>
              <Radio label="" align="right">全部</Radio>
            </RadioGroup>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 添加或修改库存盘点对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="620px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="12">
          <el-form-item label="盘点单号" prop="pdmDjbh">
            <el-input
              v-model="form.pdmDjbh"
              placeholder="流水单号生成"
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="盘点日期" prop="pdmPdrq">
            <el-date-picker
              clearable
              size="small"
              v-model="form.pdmPdrq"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择盘点日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="盘点仓库" prop="pdmCkdm">
            <el-select
              v-model="form.pdmCkdm"
              :disabled="title == '修改库存盘点'"
              placeholder="请选择盘点仓库"
              style="width: 450px"
              clearable
            >
              <el-option
                v-for="item in storageOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="盘点客户" prop="pdmKhdm">
            <el-select
              v-model="form.pdmKhdm"
              placeholder=""
              disabled
              style="width: 450px"
              clearable
            >
              <el-option
                v-for="item in storageOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="pdmDesc">
            <el-input
              type="textarea"
              v-model="form.pdmDesc"
              placeholder="请输入备注"
            />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPdmMstr,
  getPdmMstr,
  delPdmMstr,
  addPdmMstr,
  updatePdmMstr,
  listPddDet,
  exportFirm,
  submitPdmMstr,
} from "@/api/repertory/PdmMstr";
import { getAll, getstorageType } from "@/api/repertory/Storage";
import { getAllCustomer, getAllCustomerTwo } from "@/api/project/customer";
import { exportTable } from "@/utils/ruoyi";

export default {
  name: "PdmMstr",
  data() {
    return {
      tableH: (document.body.clientHeight - 130) / 2,
      tableHChild: (document.body.clientHeight - 280) / 2,
      iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
        //提交
        if (row.pdmZtbz == "1" && row.pdmTjflag == "1") {
          return "padding:0px 0;color:#0fac09";
        }
        //确认
        else if (row.pdmZtbz == "2" && row.pdmTjflag == "1") {
          return "padding:0px 0;color:#0323af";
        }
        //批准
        else if (row.pdmZtbz == "4" && row.pdmTjflag == "1") {
          return "padding:0px 0;color:#a503bb";
        }
        //过账
        if (row.rwmZtbz == "6" && row.pdmTjflag == "1") {
          return "padding:0px 0;color:#bb0331";
        }
        //子表
        if (row.pddPdsl >= 0) {
          return "padding:0px 0;color:#bb0331";
        }
      },

      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      clickData: {},
      // 非单个禁用
      single: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      // 库存盘点表格数据
      PdmMstrList: [],
      pddDetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 开单日期时间范围
      dateRange: [
        this.$moment().add(-1, "month").startOf("month").format(),
        this.$moment().add(2, "month").endOf("month").format(),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pdmDjbh: null,
        pdmCkdm: null,
        pdmZtbz: "1",
        pdmJlrq: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pdmCkdm: [
          { required: true, message: "盘底仓库不能为空", trigger: "blur" },
        ],
        pdmPdrq: [
          { required: true, message: "盘底日期不能为空", trigger: "blur" },
        ],
      },
      /** 字典数据 **/
      //有效仓库字典
      storageOptions: [],
      //所有仓库字典
      allStorageOptions: [],
      //客户类型
      customerOptions: [],
      //所有客户类型
      allCustomerOptions: [],
      //单位
      unitFlagOptions: [],
    };
  },
  created() {
    //有效仓库
    getstorageType("0").then((response) => {
      this.storageOptions = response.data;
      for (let item of this.storageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    //单位
    this.getAllDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
    //所有仓库
    getAll().then((response) => {
      this.allStorageOptions = response.data;
      for (let item of this.allStorageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });

    getAllCustomer().then((response) => {
      for (let i of response.data) {
        i.dictLabel = i.cusName;
        i.dictValue = i.cusId + "";
        i.listClass = "default";
      }

      this.customerOptions = response.data;
    });

    getAllCustomerTwo().then((response) => {
      for (let i of response.data) {
        i.dictLabel = i.cusName;
        i.dictValue = i.cusId + "";
        i.listClass = "default";
      }

      this.allCustomerOptions = response.data;
    });

    this.getList();
  },
  methods: {
    /** 查询库存盘点列表 */
    getList(row) {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.dateRange && "" != this.dateRange) {
        this.queryParams.params["beginPdmJlrq"] = this.moment(
          this.dateRange[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endPdmJlrq"] = this.moment(
          this.dateRange[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      this.queryParams.pageNum = 1;
      listPdmMstr(this.queryParams).then((response) => {
        this.PdmMstrList = [];
        this.pddDetList = [];
        this.PdmMstrList = this.PdmMstrList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
        if (row != null) {
          this.clickRow(row);
        } else if (this.PdmMstrList.length > 0) {
          this.clickRow(this.PdmMstrList[0]);
        }
      });
    },

    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 25;
        this.queryParams.pageNum++;
        listPdmMstr(this.queryParams).then((response) => {
          this.PdmMstrList = this.PdmMstrList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pdmId: null,
        pdmDjbh: null,
        pdmPdrq: new Date(),
        pdmKhdm: null,
        pdmCkdm: null,
        pdmDesc: null,
        pdmNgdesc: null,
        pdmTjflag: null,
        pdmZtbz: null,
        pdmJlry: null,
        pdmJlrq: null,
        pdmQrry: null,
        pdmQrrq: null,
        pdmPzry: null,
        pdmPzrq: null,
        pdmGzry: null,
        pdmGzrq: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.PdmMstrList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    clickRow(row) {
      this.clickData = row;
      this.single = row == null || row.pdmZtbz != "1" || row.pdmTjflag != 0;
      //获得选中的详细数据
      this.getPddDetList(row.pdmDjbh);
    },
    getPddDetList(pdmDjbh) {
      this.pddDetList = [];
      listPddDet(pdmDjbh).then((response) => {
        this.pddDetList = response.data;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库存盘点";
    },
    /** 修改按钮操作 */
    handleUpdate() {
      this.reset();
      const pdmId = this.clickData.pdmId;
      getPdmMstr(pdmId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库存盘点";
      });
    },
    handleSearch() {
      this.reset();
      this.title = "查询库存盘点";
      this.openSearch = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.pdmId != null) {
            updatePdmMstr(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPdmMstr(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete() {
      const pdmIds = this.clickData.pdmId;
      this.$confirm(
        '是否确认删除库存盘点单号"' + this.clickData.pdmDjbh + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delPdmMstr(pdmIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 提交按钮操作 */
    handleSubmit() {
      submitPdmMstr(this.clickData.pdmId).then((res) => {
        if (res.code == "200") {
          this.getList();
          this.msgSuccess("提交成功");
        }
      });
    },
    /** 导出按钮操作 */
    handleExport(a, b) {
      exportTable(a, b);
    },
    exportTotal() {
      const order = this.clickData.pdmDjbh;
      this.$confirm("是否确认导出?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportFirm({ pdmDjbh: order });
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        });
    },
  },
};
</script>

<style lang="scss" scoped></style>
